package off_imp

import (
	"bp/internal/service/com/offer"
	offGrpc "bp/rpc/gen/backendapi/off"
	messageGrpc "bp/rpc/gen/shared/message"
)

// 以下 pb -> entity
type TransactOrderItemPtrPbToEntityFunc func(*offGrpc.TransactOrderItemEntity) *offer.ToLockTransactOrderItem
type TransactOrderItemPtrArrayPbToEntityFunc func([]*offGrpc.TransactOrderItemEntity) []*offer.ToLockTransactOrderItem
type TransactOrderPtrPbToEntityFunc func(*offGrpc.TransactOrderEntity) *offer.ToLockTransactOrder
type TransactOrderPtrArrayPbToEntityFunc func([]*offGrpc.TransactOrderEntity) []*offer.ToLockTransactOrder

// ConvTransactOrderItemPtrPbToEntity get func of *offGrpc.TransactOrderItemEntity => *offer.ToLockTransactOrderItem
func (s *OffBackendApiImp) ConvTransactOrderItemPtrPbToEntity() TransactOrderItemPtrPbToEntityFunc {
	return func(ptr *offGrpc.TransactOrderItemEntity) *offer.ToLockTransactOrderItem {
		if ptr == nil {
			return nil
		}
		return &offer.ToLockTransactOrderItem{
			DetailID:   ptr.DetailId,
			DetailCnt:  ptr.DetailCnt,
			ItemID:     ptr.ItemId,
			ItemSpecID: ptr.ItemSpecId,
			ItemPrice:  ptr.ItemPrice,
		}
	}
}

// ConvTransactOrderItemPtrArrayPbToEntity get func of []*offGrpc.TransactOrderItemEntity => []*offer.ToLockTransactOrderItem
func (s *OffBackendApiImp) ConvTransactOrderItemPtrArrayPbToEntity() TransactOrderItemPtrArrayPbToEntityFunc {
	return func(ptrs []*offGrpc.TransactOrderItemEntity) (vPtrs []*offer.ToLockTransactOrderItem) {
		for _, ptr := range ptrs {
			vPtrs = append(vPtrs, s.ConvTransactOrderItemPtrPbToEntity()(ptr))
		}
		return
	}
}

// ConvTransactOrderPtrPbToEntity get func of *offGrpc.TransactOrderEntity => *offer.ToLockTransactOrder
func (s *OffBackendApiImp) ConvTransactOrderPtrPbToEntity() TransactOrderPtrPbToEntityFunc {
	return func(ptr *offGrpc.TransactOrderEntity) *offer.ToLockTransactOrder {
		if ptr == nil {
			return nil
		}
		return &offer.ToLockTransactOrder{
			OwnerID:       ptr.OwnerId,
			OutOrderNo:    ptr.OutOrderNo,
			OrderItemPtrs: s.ConvTransactOrderItemPtrArrayPbToEntity()(ptr.Items),
		}
	}
}

// ConvTransactOrderPtrArrayPbToEntity get func of []*offGrpc.TransactOrderEntity => []*offer.ToLockTransactOrder
func (s *OffBackendApiImp) ConvTransactOrderPtrArrayPbToEntity() TransactOrderPtrArrayPbToEntityFunc {
	return func(ptrs []*offGrpc.TransactOrderEntity) (vPtrs []*offer.ToLockTransactOrder) {
		for _, ptr := range ptrs {
			vPtrs = append(vPtrs, s.ConvTransactOrderPtrPbToEntity()(ptr))
		}
		return
	}
}

// 以下 entity -> pb

type OffTransactOffItemPtrEntityToPbFunc func(*offer.OffTransactionOfferItem) *offGrpc.OffTransactionOfferItemEntity
type OffTransactOffItemPtrArrayEntityToPbFunc func([]*offer.OffTransactionOfferItem) []*offGrpc.OffTransactionOfferItemEntity
type OffTransactOrderPtrEntityToPbFunc func(*offer.OffTransactionOrder) *offGrpc.OffTransactionOrderEntity
type OffTransactOrderPtrArrayEntityToPbFunc func([]*offer.OffTransactionOrder) []*offGrpc.OffTransactionOrderEntity

// ConvTransactOrderItemPtrEntityToPb get func of *offer.OffTransactionOfferItem => *offGrpc.OffTransactionOfferItemEntity
func (s *OffBackendApiImp) ConvOffTransactOffItemPtrEntityToPb() OffTransactOffItemPtrEntityToPbFunc {
	return func(ptr *offer.OffTransactionOfferItem) *offGrpc.OffTransactionOfferItemEntity {
		if ptr == nil {
			return nil
		}
		return &offGrpc.OffTransactionOfferItemEntity{
			Off:           ptr.Off,
			OfferType:     ptr.OfferType,
			OfferItemType: ptr.OfferItemType,
			OfferItemId:   ptr.OfferItemID,
			OfferItemInfo: &messageGrpc.OffItemInfoEntity{
				Name: ptr.OfferItemInfoPtr.Name,
				Desc: ptr.OfferItemInfoPtr.Desc,
			},
		}
	}
}

// ConvTransactOrderItemPtrArrayEntityToPb get func of []*offer.OffTransactionOfferItem => []*offGrpc.OffTransactionOfferItemEntity
func (s *OffBackendApiImp) ConvOffTransactOffItemPtrArrayEntityToPb() OffTransactOffItemPtrArrayEntityToPbFunc {
	return func(ptrs []*offer.OffTransactionOfferItem) (vPtrs []*offGrpc.OffTransactionOfferItemEntity) {
		for _, ptr := range ptrs {
			vPtrs = append(vPtrs, s.ConvOffTransactOffItemPtrEntityToPb()(ptr))
		}
		return
	}
}

// ConvOffTransactOrderPtrEntityToPb get func of *offer.OffTransactionOrder => *offGrpc.OffTransactionOrderEntity
func (s *OffBackendApiImp) ConvOffTransactOrderPtrEntityToPb() OffTransactOrderPtrEntityToPbFunc {
	return func(ptr *offer.OffTransactionOrder) *offGrpc.OffTransactionOrderEntity {
		if ptr == nil {
			return nil
		}
		return &offGrpc.OffTransactionOrderEntity{
			OwnerId:    ptr.OwnerID,
			OutOrderNo: ptr.OutOrderNo,
			Off:        ptr.Off,
			OfferItems: s.ConvOffTransactOffItemPtrArrayEntityToPb()(ptr.OfferItemPtrs),
		}
	}
}

// ConvOffTransactOrderPtrArrayEntityToPb get func of []*offer.OffTransactionOrder => []*offGrpc.OffTransactionOrderEntity
func (s *OffBackendApiImp) ConvOffTransactOrderPtrArrayEntityToPb() OffTransactOrderPtrArrayEntityToPbFunc {
	return func(ptrs []*offer.OffTransactionOrder) (vPtrs []*offGrpc.OffTransactionOrderEntity) {
		for _, ptr := range ptrs {
			vPtrs = append(vPtrs, s.ConvOffTransactOrderPtrEntityToPb()(ptr))
		}
		return
	}
}
